package com.qinghuan.threads.chapterOne;

/**
 * @Author: dubao
 * @Date: 2022/5/17 19:58
 * @Description:
 *
 * 线程使用Interrupted优雅退出的经典例子
 */
public class Test1_7_1 {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                //如果单签线程被中断，则退出
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread() + " hello");
                }
            }
        });
        thread.start();
        Thread.sleep(1000);

        System.out.println("main thread interrput thread");
        thread.interrupt();
        System.out.println("after interrupt");
        thread.join();
        System.out.println("main is over!");
    }

}
